unit Importa_Arquivo_FolhaGerasoft;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, Formulario_Padrao_Rel, StdCtrls, RDprint, ExtCtrls, CheckLst,
  ComCtrls, TFlatButtonUnit, Buttons, FileCtrl, Mask, ToolEdit, DB,
  DBClient,Biblio;

type
  Timporta_arq_folhagerasoft = class(TFrmPad_Rel)
    Label2: TLabel;
    filearq: TFilenameEdit;
    edtconta: TEdit;
    btnC_ContaCorrente: TSpeedButton;
    Panel22: TPanel;
    edtHist_codigo: TMaskEdit;
    btnC_Historico: TSpeedButton;
    Panel8: TPanel;
    Label1: TLabel;
    cds: TClientDataSet;
    cdshist_codigo: TStringField;
    cdsmov_favorecido: TStringField;
    cdsmov_numcheque: TIntegerField;
    cdsconta_codigo: TStringField;
    cdsmov_valor: TStringField;
    cdsmov_datamov: TStringField;
    procedure bimprimirClick(Sender: TObject);
    procedure edtcontaKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure edtcontaExit(Sender: TObject);
    procedure edtcontaEnter(Sender: TObject);
    procedure edtHist_codigoEnter(Sender: TObject);
    procedure edtHist_codigoKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure edtHist_codigoExit(Sender: TObject);
    procedure btnC_HistoricoClick(Sender: TObject);
    procedure btnC_ContaCorrenteClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormActivate(Sender: TObject);
    procedure btncancelarClick(Sender: TObject);
    procedure filearqKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  importa_arq_folhagerasoft: Timporta_arq_folhagerasoft;

implementation
uses clsparametros,clsmovim,clshistorico,clscontacorrente,Con_ContaCorrente,Con_Historico;

var
  parametros      : TParametros;
  mov             : TMovim;
  conta           : TContaCorrente;
  historico       : THistorico;
  caminho         : string;
  f3              : TfrmCon_Historico;
  f5              : TfrmCon_ContaCorrente;

{$R *.dfm}

procedure Timporta_arq_folhagerasoft.bimprimirClick(Sender: TObject);
var
  Arq: textfile;
  linha: String;
begin
  inherited;
  mov := TMovim.create;
  cds.Close;
  cds.CreateDataSet;
  cds.Open;
  AssignFile(Arq,filearq.Text);
  Reset(Arq);

  while not eof(Arq) do begin
   readln(arq,linha);
   cds.Append;
    cdshist_codigo.asstring := edthist_codigo.text;
    cdsmov_datamov.asstring := copy(linha,4,8); // data
    cdsmov_numcheque.asinteger := strtoint(copy(linha,18,6));    // n. cheque
    cdsmov_favorecido.asstring := copy(linha,24,40); // funcionario
    cdsmov_valor.asstring := copy(linha,64,11); //valor     
    cdsconta_codigo.asstring := edtconta.text;
   cds.post;
  end;

  cds.first;
  while not cds.eof do begin
   mov.Hist_Codigo    :=  cdshist_codigo.asstring;
   mov.Mov_DataMov    :=  StrToDate(botabarr(cdsmov_datamov.asstring));
   mov.Mov_Valor      :=  (StrToFloat(cdsmov_valor.asstring)/100)*-1;
   mov.Mov_Favorecido :=  cdsmov_favorecido.asstring;
   mov.Mov_Historico  :=  cdsmov_favorecido.asstring;
   mov.Mov_NumCheque  :=  cdsmov_numcheque.asinteger;
   mov.Conta_Codigo   :=  cdsconta_codigo.asstring;
   mov.Mov_Tic        := 'N';
   mov.Mov_ImpCheque  := 'N';
   mov.Mov_Contab     := 'N';
   mov.Mov_DataTic    := 0;
   mov.Gravar(mov.ultimo);
   cds.next;
  end;

  CloseFile(Arq);
  mov.free;
  cds.EmptyDataSet;
  formactivate(sender);
end;

procedure Timporta_arq_folhagerasoft.edtcontaKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  if key=vk_f2 then btnC_ContaCorrenteClick(sender);
   if key=12 then begin
    if trim(edtConta.text)<>'' then begin
     Conta:= TContaCorrente.Create;
     if Conta.Recuperar(edtconta.text)=0 then begin
      panel22.caption:= '';
     end else begin
      panel22.caption:= Conta.Conta_responsavel;
     end;
     Conta.free;
    end;
 end;
end;

procedure Timporta_arq_folhagerasoft.edtcontaExit(Sender: TObject);
begin
  inherited;
  sb.panels[0].text         := '';
end;

procedure Timporta_arq_folhagerasoft.edtcontaEnter(Sender: TObject);
begin
  inherited;
  btnC_ContaCorrente.enabled:= true;
  sb.panels[0].text         := 'F2 Consulta';
end;

procedure Timporta_arq_folhagerasoft.edtHist_codigoEnter(Sender: TObject);
begin
  inherited;
  btnC_Historico.enabled := true;
  sb.panels[0].text      := 'F2 Consulta';
end;

procedure Timporta_arq_folhagerasoft.edtHist_codigoKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  if key=vk_f2 then btnC_historicoClick(sender);
  if key=12 then begin
   if trim(edtHist_Codigo.text)<>'' then begin
    Historico:= THistorico.Create;
    if Historico.Recuperar(edtHist_codigo.text)=0 then begin
     panel8.caption:= '';
    end else begin
     panel8.caption:= Historico.Hist_descricao;
    end;
    Historico.free;
   end;
  end;
end;

procedure Timporta_arq_folhagerasoft.edtHist_codigoExit(Sender: TObject);
begin
  inherited;
  sb.Panels[0].Text           := '';
end;

procedure Timporta_arq_folhagerasoft.btnC_HistoricoClick(Sender: TObject);
begin
  inherited;
  f3 := TfrmCon_Historico.Create(Self);
  f3.ShowModal;
  EdtHist_Codigo.text:= f3.Hist_Codigo;
  f3.free;
end;

procedure Timporta_arq_folhagerasoft.btnC_ContaCorrenteClick(
  Sender: TObject);
begin
  inherited;
  f5 := TfrmCon_ContaCorrente.Create(Self);
  f5.ShowModal;
  edtConta.text:= f5.Conta_codigo;
  f5.free;
  edtConta.SetFocus;
end;

procedure Timporta_arq_folhagerasoft.FormShow(Sender: TObject);
begin
  inherited;
  parametros := TParametros.create;
  parametros.Recuperar('1');
  if parametros.Mascara_Historico<>'' then begin
    edthist_codigo.EditMask:= parametros.Mascara_Historico+';1; ';
  end;
  parametros.free;
end;

procedure Timporta_arq_folhagerasoft.FormActivate(Sender: TObject);
begin
  inherited;
  panel22.caption := '';
  panel8.caption  := '';
  edtconta.clear;
  edtHist_codigo.Clear;
  btnC_Historico.enabled := false;
  filearq.Clear;
end;

procedure Timporta_arq_folhagerasoft.btncancelarClick(Sender: TObject);
begin
  inherited;
   formactivate(sender);
   bimprimir.enabled := false;
end;

procedure Timporta_arq_folhagerasoft.filearqKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  inherited;
  bimprimir.enabled := true;
end;

end.
